//
//  XkxSportMapController.m
//  侠客行
//
//  Created by laibingqiang on 16/11/8.
//  Copyright © 2016年 laibingqiang. All rights reserved.
//

#import "XkxSportMapController.h"
#import <MAMapKit/MAMapKit.h>
@interface XkxSportMapController () <MAMapViewDelegate>

@end

@implementation XkxSportMapController{
    
    CLLocation *_startLocation;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupMapInfo];
}
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if(!updatingLocation)
    {
        return;
    }
    
    if(_startLocation == nil)
    {
        _startLocation = userLocation.location;
        
        MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
        
        annotation.coordinate = userLocation.coordinate;
        
        [mapView addAnnotation:annotation];
    }
    
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if(![annotation isKindOfClass:[MAPointAnnotation class]])
    {
        return nil;
    }
    
    static NSString *reuseidy = @"reuseidy";
    
    MAAnnotationView *anV = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseidy];
    if(anV == nil)
    {
        anV = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseidy];
    }
    UIImage *img = _sportModel.image;
    
    anV.image = img;
    
    anV.centerOffset = CGPointMake(0, -img.size.height * 0.5);
    
    
    return  anV;
}

- (void)setupMapInfo
{
    ///初始化地图
    MAMapView *mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    
    ///把地图添加至view
    [self.view addSubview:mapView];
    //关闭比例尺
    mapView.showsScale = NO;
    //相机旋转
    mapView.rotateCameraEnabled = NO;
    //显示用户位置
    mapView.showsUserLocation = YES;
//    //用户追踪模式
    mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    mapView.pausesLocationUpdatesAutomatically = NO;
    
    mapView.allowsBackgroundLocationUpdates = YES;//iOS9以上系统必须配置
    
    mapView.delegate = self;
}


@end
